<?php

namespace app\admin\controller;

use think\Controller;
use buildView\Detail;
use buildView\Form;
use buildView\Grid;
use app\common\model\ShopAgent;
use think\Db;

class Agent extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopAgent);
		$grid->column('username','账号');
		$grid->column('phone','手机号');
		$grid->column('region','代理区域');
		$grid->column('address','详细地址');
        $grid->column('status', '状态')->align('left')->switchs([
            'on' => ['text' => '启用', 'value' => 1],
            'off' => ['text' => '禁用', 'value' => 0],
        ]);
		$grid->column('create_at','添加时间');
		$grid->filter(function ($filter){
            $filter->dateBetween('create_at','添加时间');
        });
        $grid->hideExportButton();
        $grid->actions(function ($action){
        	 $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 详情
     * @auth true
     * @menu true
     * @return string
     */
    public function detail()
    {
        $detail = new Detail(new ShopAgent);
		$detail->column('username','账号');
        $detail->column('name','姓名');
        $detail->column('phone','手机号');
		$detail->column('region','代理区域');
		$detail->column('login_at','登录时间');
		$detail->column('login_ip','登录IP');
		$detail->column('login_num','登录次数');
		$detail->column('address','详细地址');
		$detail->column('desc','备注说明');
		$detail->column('create_at','添加时间');
        return $detail->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $form = new Form(new ShopAgent);
		$form->text('username','代理商账号')->required()->help('默认登录密码为123456');
        $form->text('name','代理商姓名')->required();
        $form->text('phone','代理商手机')->required();
        $form->distpicker(['province','city','area'],'代理区域')->required();
		$form->text('address','详细地址')->required();
        $form->text('desc','备注');
        //提交保存前回调
        $form->saving(function($post){
            if (Db::name('shop_agent')->where(['username' => $post['username'], 'is_deleted' => '0'])->count() > 0) {
                $this->error("账号{$post['username']}已存在");
            }
            if (Db::name('shop_agent')->where(['phone' => $post['phone'], 'is_deleted' => '0'])->count() > 0) {
                $this->error("手机号已存在");
            }
            $post['password'] = md5('123456');
            if (array_key_exists('province',$post)){
                $post['region'] = $post['province'];
                $post['type'] = 1;
            }
            if (array_key_exists('city',$post)){
                $post['region'] = $post['city'];
                $post['type'] = 2;
            }
            if (array_key_exists('area',$post)){
                $post['region'] = $post['area'];
                $post['type'] = 3;
            }
            return $post;
        });
        return $form->view();
    }
}
